/**
 * This class has all the Invertory controls like adding and deleting an item
 */
public class InventoryController {
        InventoryModel model = new InventoryModel();
        //InventoryView view = new InventoryView();
        /**
         * This constructor takes in InventoryModel as a parameter and
         * instantiates it
         * @param model 	model is an instance of InventoryModel
         */
        public InventoryController(InventoryModel model) //, InventoryView view) {
        {
                this.model = model;
              //  this.view = view;
        }

        /**
         * This method adds an Item to the arrayList of items, increments the weight of the
         * wagon.
         * @param type		type is the type of the Item
         * @param quantity	quantity is the amount of the Item
         */
        public void addNewItem(String type, int quantity) {
                Item newItem = new Item(type, quantity);
                model.getItems().add(newItem);
                double weight = newItem.getWeight();
                model.setTotalLbs(model.getTotalLbs() + weight);

        }

        /**
         * This method deletes the item from arraylist and decrements the total weight
         * @param type		type is the type of the Item
         * @param quantity	quantity is the amount of the Item
         */
        public void deleteItem(String type, int quantity) {
                for (int i = 0; i <= model.getItems().size(); i++) {
                        if (model.getItems().get(i).getType() == type) {
                                double weight = model.getItems().get(i).getWeight();
                                model.getItems().remove(i);
                                model.setTotalLbs(model.getTotalLbs() - weight);
                        }
                }
        }

        // increases the quantity of the particular item in the arrayList and also
        // increases the total weight of wagon.
        public void incrementQuantityOfItem(String type, int incrementValue) {
                for (int i = 0; i < model.getItems().size(); i++) {
                        if (model.getItems().get(i).getType().equalsIgnoreCase(type)) {
                                model.getItems().get(i).setQuantity(
                                                (model.getItems().get(i).getQuantity()) + incrementValue);
                                double itemWeight = model.getItems().get(i).getWeight();
                                model.setTotalLbs(model.getTotalLbs() + itemWeight);
                        }
                }
        }

        /**
         * Decreases the quantity of that particular object and also decreases the
         * total weight of the wagon
         * @param type		type is the type of the Item
         */
        public void decrementQuantityOfItem(String type) {
                for (int i = 0; i < model.getItems().size(); i++) {
                	if (model.getItems().get(i).getType().equalsIgnoreCase(type)) {
                                model.getItems().get(i).setQuantity(
                                                (model.getItems().get(i).getQuantity()) - 1);
                                double itemWeight = model.getItems().get(i).getWeight();
                                model.setTotalLbs(model.getTotalLbs() - itemWeight);
                        }

                }
        }
        /**
         * @param type 		type is the type of the Item
         * @return int		int is the quantity of the Items
         */
        public int getQuantityOfItem(String type) {
            for (int i = 0; i < model.getItems().size(); i++) 
            	if (model.getItems().get(i).getType().equalsIgnoreCase(type)) 
                            return model.getItems().get(i).getQuantity();
            
            return -1;
        }

}